<?php 
include('../../../include/db.inc.php');
include('../../../cfg/cfg.inc.php');

session_start();

$db=new DbManager();
$db->connect(DBHOST, DBUSER, DBPWD,true,true,DBNAME,"");

function getFieldValue($db,$tableName,$pkRecord,$fieldName)
{
    $sql="SELECT A.NAME,B.VALUE FROM cllg_tablefield A LEFT JOIN
            cllg_table_valuebyfield B ";
    $sql.=sprintf("ON (A.NAME=B.FK_TABLEFIELD AND B.PK_RECORD='%s') ",$pkRecord);
    $sql.=sprintf("WHERE B.FK_TABLEFIELD='%s' ",$fieldName);
    $sql.=sprintf("AND A.TABLE_NAME='%s' ",$tableName);
    //echo $sql;
    $rs=$db->fetch_all_array($db->query($sql));

    return $rs;
}
$fkEntity = 2;
$Carre = $_SESSION["cli1"];
$ciclo = $_SESSION["cli2"];

$queryEstudiantes = "select cllg_student.ID CARNET,LAST_NAME,FIRST_NAME,
SECOND_NAME,BIRTHDATE,cllg_student_assign.CREATED,cllg_student_assign.ID
from
      cllg_student,cllg_student_assign
where  ";

if(isset($_SESSION["cli1"])){
   $queryEstudiantes.=" cllg_student_assign.FK_CAREER = '$Carre' AND ";
}

$queryEstudiantes.=" cllg_student_assign.STATUS=1
        and cllg_student_assign.FK_CYCLE = '$ciclo'
        AND cllg_student_assign.FK_ENTITY = '$fkEntity'
        AND cllg_student_assign.FK_STUDENT = cllg_student.ID ";

$daysWhere="and cllg_student_assign.ID in ( select a.`ID`
                from cllg_student_assign a, cllg_assign_detail b, cllg_teacher_assign c
               where
                    a.ID=b.FK_ASSIGN and b.SECTION=c.SECTION
                    and b.FK_COURSE=c.FK_COURSE ";
$where="";
$where=isset($_SESSION["LUNES"])?$where." AND LUNES = 1":$where;
$where=isset($_SESSION["MARTES"])?$where." AND MARTES = 1":$where;
$where=isset($_SESSION["MIERCOLES"])?$where." AND MIERCOLES = 1":$where;
$where=isset($_SESSION["JUEVES"])?$where." AND JUEVES = 1":$where;
$where=isset($_SESSION["VIERNES"])?$where." AND VIERNES = 1":$where;
$where=isset($_SESSION["SABADO"])?$where." AND SABADO = 1":$where;

if(strlen($where)>0){
    $where=$daysWhere.$where.")";
}
$order=" ORDER BY LAST_NAME,FIRST_NAME";

$studentsResult = $db->queryToArray($queryEstudiantes.$where.$order);
//echo $db->lastQuery;
$Fec = date('Y');
?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
        <title>Alumnos Por Carrera</title>
        <link href="css/print.css" rel="stylesheet" type="text/css" />
       <script type="text/javascript" src="../../../js/prototype.js">
</script>
    </head>

    <body>
        <div id="wrap">

            <div id="header">
                <h1>Listado Alumnos </h1>
            </div>
            <div id="content">
                <h2><?=$_GET["carrera"];?></h2>
                <?php if(strlen($where)){ ?>
                <h2>Dias: <?=isset($_SESSION["LUNES"])?"LUNES,":""?>
                <?=isset($_SESSION["MARTES"])?"MARTES,":""?>
                <?=isset($_SESSION["MIERCOLES"])?"MIERCOLES,":""?>
                <?=isset($_SESSION["JUEVES"])?"JUEVES,":""?>
                <?=isset($_SESSION["VIERNES"])?"VIERNES,":""?>
                <?=isset($_SESSION["SABADO"])?"SABADO":""?>
                </h2>
                <?php } ?>
                <table width="296">
                    <tr>
                        <td id="noprint"><div id="no">
    <form id="form1" name="form1" method="post" action="">
      <input type="button" name="printer" id="printer" value="Imprimir" onclick="$('no').hide();window.print();alert('Impresion Lista!');$('no').show();" />
        </form>
  </div>
                        </td>
                    </tr>
                </table>
                <table width="100%" border="0" cellspacing="0" cellpadding="0">

                    <tr>
                        <td colspan="8"><table width="100%" border="0">
                                <tr>
                                    <td width="5%"><strong>Codigo</strong></td>
                                    <td width="22%"><strong>Nombre Alumno </strong></td>
                                    <td width="4%"><strong>Edad</strong></td>
                                    <td width="16%"><strong>Email</strong></td>
                                    <td width="11%"><strong>Telefonos</strong></td>
                                    <td width="21%"><strong>Empresa</strong></td>
                                    <td width="13%"><strong>Telefono Empresa</strong></td>
                                    <td width="8%"><strong>Tipo Sangre </strong></td>
                                </tr>
                                <?PHP
                                $c=0;
                                foreach($studentsResult as $rowStudent ){
                                    $c++;
                                    $mail=getFieldValue($db, "cllg_student",$rowStudent["CARNET"],"EMAIL");
                                    $t1=getFieldValue($db, "cllg_student",$rowStudent["CARNET"],"CELL_PHONE");
                                    $t2=getFieldValue($db, "cllg_student",$rowStudent["CARNET"],"HOME_TELEPHONE");
                                    $t4=getFieldValue($db, "cllg_student",$rowStudent["CARNET"],"WORK_TELEPHONE");
                                    $empresa=getFieldValue($db, "cllg_student",$rowStudent["CARNET"],"COMPANY_NAME");
                                    $tsangre=getFieldValue($db, "cllg_student",$rowStudent["CARNET"],"TIPO_SANGRE");
                                    ?><tr>
                                    <td height="28"><?= $c?></td>
                                    <td width="22%">
                                        <span class="Estilo1"><?PHP echo $rowStudent['LAST_NAME'];?>,&nbsp;
                                            <?PHP echo $rowStudent['FIRST_NAME'];?>
                                            <?PHP echo $rowStudent['SECOND_NAME'];?></span> </td>
                                    <td><?PHP echo $Fec-date('Y',strtotime($rowStudent['BIRTHDATE']));?></td>
                                    <td><?PHP echo $mail[0]["VALUE"];?></td>
                                    <td><?PHP echo $t1[0]["VALUE"];?>&nbsp;-&nbsp;<?PHP echo $t2[0]["VALUE"];?>&nbsp;                                    </td>
                                    <td><?PHP echo $empresa[0]["VALUE"];?></td>
                                    <td><?PHP echo $t4[0]["VALUE"];?></td>
                                    <td><?PHP echo $tsangre[0]["VALUE"];?></td>
                                </tr><?php }?>
                        </table></td>
                    </tr>
                    <tr>
                        <td width="14%">&nbsp;</td>
                        <td width="28%">&nbsp;</td>
                        <td width="6%">&nbsp;</td>
                        <td width="33%">&nbsp;</td>
                        <td width="76%" colspan="4" rowspan="2">&nbsp;</td>
                    </tr>
                    <tr>
                        <td>&nbsp;</td>
                        <td>&nbsp;</td>
                        <td>&nbsp;</td>
                        <td>&nbsp;</td>
                    </tr>
                </table>
                <hr />
            </div>
            <div id="footer">CINDE  - Generado el <?PHP echo $Fec;?></div>
        </div>
    </body>
</html>